# !/usr/bin/env python3
# -*- coding: utf-8 -*-


import os
import sys
from util.json_utils import json_file_to_xml_file
from util.json_utils import json_file_to_xml_file_without_root
from util.solution_adapter import translating_solution, transforming_solution
from util.flow_adapter import transforming_flow_feature
from util.topo_adapter import transforming_topo_feature
from util.json_utils import xml_file_to_json_file

dir_mytest = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, dir_mytest)


def processing(file_path='../input/'):
    print('1. 将json文件转化为xml文件 >>>')
    # json输入文件
    flow_json = file_path + 'tsnbuilder/flow_feature.json'
    topo_json = file_path + 'tsnbuilder/topo_feature.json'
    para_json = file_path + 'tsnbuilder/cfg_parameter.json'
    mac_json = file_path + 'tsnbuilder/static_mac_cfg.json'
    test_aux_json = file_path + 'tsnbuilder/test_aux.json'
    forward_mode_json = file_path + 'tsnbuilder/forward_mode.json'

    # 将json输入文件转为xml输入文件
    json_file = flow_json
    json_file_to_xml_file_without_root(json_file, 'flow_feature', 'flow')
    json_file = topo_json
    json_file_to_xml_file_without_root(json_file, 'topo_feature', 'entry')
    json_file = para_json
    json_file_to_xml_file(json_file)
    json_file = mac_json
    json_file_to_xml_file(json_file)
    json_file = test_aux_json
    json_file_to_xml_file(json_file)
    json_file = forward_mode_json
    json_file_to_xml_file(json_file)

    print('2. 将solution.json文件转化为solution.xml文件 >>>')
    # 输入文件
    flow_file = file_path + 'tsnbuilder/flow_feature.xml'
    topo_file = file_path + 'tsnbuilder/topo_feature.xml'
    solution_json = file_path + 'algorithm/frame/solution.json'
    # 输出文件
    solution_file = file_path + 'tsnbuilder/plan.xml'
    stream_txt = '../tmp/stream.txt'
    topo_txt = '../tmp/topo.txt'

    # 将solution.json转换为将plan.xml
    transforming_topo_feature(topo_file, topo_txt)
    transforming_flow_feature(flow_file, topo_file, stream_txt)
    translating_solution(topo_txt, stream_txt, solution_json, solution_file)
    transforming_solution(solution_json, solution_file)

    # 将plan.xml转成plan.json
    xml_file_to_json_file(solution_file)

